package com.me.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.socket.TextMessage;

import com.me.bean.JsonDateStructure;
import com.me.util.GetTIme;
import com.me.util.MapUtil;

@Service
public class GroupChatMsgDataService {

	@Autowired
	GroupService groupService; 
	@Autowired
	WebSocketHandler webSocketHandler;
	public void init(String msg,String sender,String receiver,String type){
		System.out.println("得到消息信息："+msg+" "+sender+" "+receiver);
		try{
			List result=groupService.getRemebersAccountByGroupId(receiver);
			System.out.println("得到群用户列表："+result.toString());
			String[] remebers=new String[result.size()];
					result.toArray(remebers);
			JsonDateStructure jds=new JsonDateStructure();
			jds.setDatetype(type);
			jds.setDate(GetTIme.getNowTime());
			jds.setMsgcontent(msg);
			jds.setResptype("groupchat");
			jds.setReceiver(receiver);
			jds.setSender(sender);
			
					
			for(int i=0;i<remebers.length;i++){
				
				if(webSocketHandler.isOnline(remebers[i])){
					webSocketHandler.sendMsgToUser(remebers[i], new TextMessage(jds.toJson()));
				}
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		
		
		
		
	}

}
